// pages/play/result.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    curFeel: '',
    markers: [],
    moon: 0,
    openMap: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.setData({
      order_id: options.order_id
    })
    console.log(options)
    app.userstat(this, wx.getStorageSync("openid"))
    that.getLocation() 
    that.getData()  
  },
  getData: function () {
    let that = this
    wx.showLoading({
      title: '正在努力加载中',
    })
    app.request(app.apiDomain + 'index.php?c=step&a=getOrder', {
      order_id: that.data.order_id
    }, function (res) {
      let data = res.data.data
      let time = data.end_time - data.start_time
      let time_min = time / 60
      console.log(time)
      let km = parseFloat(data.end_step * 0.6 / 1000).toFixed(3)
      km = km.substring(0, km.length - 1)
      let speed = parseFloat(data.end_step * 0.6 / time).toFixed(2).toString()
      let speed_h = speed.substring(0, speed.indexOf('.'))
      let speed_m = speed.substring(speed.indexOf('.') + 1)
      let hour = parseInt(time / 60 / 60 % 24)
      let min = parseInt(time / 60 % 60) < 10 ? '0' + parseInt(time / 60 % 60) : parseInt(time / 60 % 60)
      let sec = parseInt(time % 60) < 10 ? '0' + parseInt(time % 60) : parseInt(time % 60)
      let kaluli = parseFloat(data.end_step / 60).toFixed(2)
      let step_pin = parseInt(data.end_step / time_min)
      let markers = []
      if (data.start_address[0] != '') {
        let obj = {
          id: 0,
          longitude: data.end_address[0],
          latitude: data.end_address[1],
          iconPath: "https://resource.160nurse.com/canvas/healthy/img/play/begin.png",
          width: 32,
          height: 38
        }
        markers.push(obj)
      }
      if (data.end_address[0] != '') {
        let obj = {
          id: 1,
          longitude: data.end_address[0],
          latitude: data.end_address[1],
          iconPath: "https://resource.160nurse.com/canvas/healthy/img/play/end_new.png",
          width: 32,
          height: 38
        }
        markers.push(obj)
      }
      console.log(markers)
      that.setData({
        markers: markers
      })
      // let step_cm = parseFloat()
      data['speed_h'] = speed_h
      data['speed_m'] = speed_m
      data['hour'] = hour
      data['min'] = min
      data['sec'] = sec
      data['kaluli'] = kaluli
      data['step_pin'] = step_pin
      data['mileage'] = parseInt(res.data.data.mileage)
      console.log('所用时间', hour, min, sec, data.step_number, time_min, step_pin)
      that.setData({
        isopen: true,
        isSuccess: res.data.data.status == 2 ? false : true,
        hour_end: hour,
        min_end: min,
        sec_end: sec,
        info: data,
        km: km,
        moon: data.mood,
        steps: data.end_step
      })
      wx.hideLoading()
    })
  },
  getLocation: function () {
    let that = this;
    //选择位置，需要用户授权
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          that.setData({
            openMap: false
          })
        } else {
          that.setData({
            openMap: true
          })
        }
      }
    })
    that.getLocationData()
  },
  getLocationData: function () {
    let that = this
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude,
          // speed: 
        })
        // that.setData({
        //   markers: []
        // })
      },
    })
  },
  run_auth: function () {
    wx.openSetting({

    })
  },
  toggleFeel: function (e) {
    let that = this
    console.log(e)
    if (that.data.moon == 0) {
      that.setData({
        curFeel: e.target.dataset.id
      })
    }
  },
  // 提交心情
  submitFeel:function () {
    let that = this
    app.request(app.apiDomain + 'index.php?c=step&a=mood', {
      order_id: that.data.order_id,
      mood_type: that.data.curFeel
    }, function (res) {
      if (res.data.status == 1) {
        wx.showToast({
          title: '保存成功',
        })
        wx.redirectTo({
          url: './index',
        })
      } else {
        wx.showToast({
          title: '快来点亮您的心情',
          icon: 'none'
        })
        wx.pageScrollTo({
          scrollTop: '200',
        })
      }
    })
  },
  goto: function () {
    let that = this
    that.setData({
      isopen: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.SystemInfo(this)
  },
  getLocationData: function () {
    let that = this
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude,
          // speed: 
        })
        // that.setData({
        //   markers: []
        // })
      },
    })
  },
  get_geo: function () {
    let that = this
    that.getLocationData()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    that.getLocation()
  },
  // 分享
  onShareAppMessage: function () {
    var nickName = '';
    var userId = 0;
    try {
      nickName = wx.getStorageSync('nickname');
      userId = wx.getStorageSync('user_id');
    } catch (e) { }
    return {
      path: "/pages/play/index?user_id=" + userId,
      title: '运动挑战，让您的每一步充满激情。',
      imageUrl: app.resourceDomain + 'canvas/healthy/img/play/play_tz.png'
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
})